<?php
//Connection class - Follows singleton pattern.
require_once($_SERVER['DOCUMENT_ROOT']."/includes/definitions.php");

class DB{

	//Private var that can only be used by this class.  Stores a self object.
	private static $dbConnection;
	
	/*Toggle to use the mysqli object style or the regular version.
	If using object style, you can call somthing like this:
	$db->db->query(...); or $db->db->prepare();
	Otherwise, you'll need to use our custom built functions, like 
	$db->cleanParam(...); or $db->query(..);
	*/
	var $useObjectStyle = false;

	//So we can access our mysqli object in our script.
	public static $db;

	private function __construct(){
		$this->dbConnect();
	}
	
	public static function getInstance(){
       if (!self::$dbConnection){
            self::$dbConnection = new DB();
            return self::$dbConnection;
        }
        else{
            //echo "database connection already exists<br>";
        }
    } 
    
    private function dbConnect(){
    	
    	//Get DB Connection Information
    	require_once(SECURE."db.include.php");
    	
    	if($this->useObjectStyle){
    		//Use the object in DB FORM
    		$this->db = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME);
    	}
    	else{
    		//Procedural Style
    		$this->db = @mysqli_connect(DBHOST, DBUSER, DBPASS, DBNAME);
    	}

		if(DEBUGMODE){
			if (mysqli_connect_errno()) {
				printf("Connect failed: %s\n", mysqli_connect_error());
				exit();
			} 
		}
    }
    public function cleanParam($param){
    	if($param){
    		$cleanParam = mysqli_real_escape_string($this->db, $param);
    		return $cleanParam;
    	}
		return false; 
    }
    public function cleanParamList($paramList = array()){
    	if(is_array($paramList) && count($paramList) > 0){
    		foreach($paramList as $key=>$value){
    			$cleanValue = $this->cleanParam($value);
    			if($cleanValue){
    				$cleanArray[$key] = $cleanValue;
    			}
    		}
    	}
    	if(count($cleanArray) > 0){
    		return $cleanArray;
    	}
 		return false;
    }  
    public function qry($query){
    	if($query){
    		$result = mysqli_query($this->db, $query);
 			if(mysqli_error($this->db)){
 				return mysqli_error($this->db);
 			}
 			return $result;
    	}
    	return false;
    }
    public function printArray($array){
    	echo "<pre>";
    	print_r($array);
    	echo "</pre>";	
    }
    
    
}



?>
